package com.pl.museumguide.activities;

import java.io.File;

import com.pl.museumguide.R;
import com.pl.museumguide.qr.integrators.IntentIntegrator;
import com.pl.museumguide.qr.integrators.IntentResult;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class scanqr extends Activity {
	
	private String url;	//tutaj znajduje sie zakodowany wynik, url, nazwa, cokolwiek
	private Button activateQRButton;
	private TextView infoTextView;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.scanqr_layout);
		this.activateQRButton = (Button) findViewById(R.id.activateQRButton);
		this.infoTextView = (TextView) findViewById(R.id.infoQRText);
		addActivateQRButtonListener();
	}
	
	
	
	private void addActivateQRButtonListener() {
		this.activateQRButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {				
				IntentIntegrator ii = new IntentIntegrator(scanqr.this);
				ii.initiateScan();
			}
		});
	}



	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		try {
			IntentResult scanResult = 
			        IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
			if(scanResult.getContents()!=null){
				this.url = scanResult.getContents().toString();
				File f = new File(Environment.getExternalStorageDirectory()
						+ File.separator + "MuseumGuide" + File.separator + "Zabytki"
						+ File.separator + url);
				if(f.exists()){
					Intent wyswietlIntent = new Intent(getApplicationContext(), Wyswietl.class);
					wyswietlIntent.putExtra("nazwa", url);
					startActivity(wyswietlIntent);
				}
				else{
					infoTextView.setText("Błędny kod QR");
				}
			}
		} catch (Exception e){ 
				infoTextView.setText("Nie wprowadzono kodu QR.");
			e.printStackTrace();
		}
	}
}
